package com.liantong.client.api.action;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.liantong.client.po.Response;
import com.opensymphony.xwork2.ActionSupport;

public abstract class AbstractAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {
	private static final long serialVersionUID = -5838482207235891422L;

	/**
	 * response object will be exposed to client to populate the XML result
	 */
	private Response response;

	/**
	 * HttpServletRequest
	 */
	private HttpServletRequest servletRequest;

	/**
	 * HttpServletResponse
	 */
	private HttpServletResponse servletResponse;

	/**
	 * servletContext
	 */
	private ServletContext servletContext;

	private Integer pageNum;

	private int capacity;

	private Integer start;

	private String versionName;

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;
	}

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public String getBaseUrl() {
		return servletRequest.getScheme() + "://" + servletRequest.getServerName() + ":" + servletRequest.getServerPort()
				+ servletRequest.getContextPath();
	}

	
	public Response getModel() {
		return getResponse();
	}

	public void setServletResponse(HttpServletResponse response) {
		this.servletResponse = response;
	}

	public String ajax(String content, String type) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getPostParamter(){
		String jsonStr = null;
		BufferedReader br = null;
		ServletRequest request = getServletRequest();
		if(request == null){
			return jsonStr;
		}
		
		ServletInputStream servletInputStream;
		try {
			servletInputStream = request.getInputStream();
			br = new BufferedReader(new InputStreamReader(
					servletInputStream, "UTF-8"));
			StringBuffer sb = new StringBuffer();
			String temp;
			while ((temp = br.readLine()) != null) {
				sb.append(temp);
			}
			jsonStr = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
					br = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return jsonStr;
	}

	public String getParameter(String name) {
		return getServletRequest().getParameter(name);
	}

	public void setAttribute(String name, Object value) {
		getServletRequest().setAttribute(name, value);
	}

	public String ajaxText(String text) {
		return ajax(text, "text/plain");
	}

	public String ajaxHtml(String html) {
		return ajax(html, "text/html");
	}

	public String ajaxXml(String xml) {
		return ajax(xml, "text/xml");
	}

	public String ajaxJson(String jsonString) {
		return ajax(jsonString, "application/json");
	}

	/**
	 * @return the servletResponse
	 */
	public HttpServletResponse getServletResponse() {
		return servletResponse;
	}

	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}

	/**
	 * @param pageNum
	 *            the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * @return the capacity
	 */
	public int getCapacity() {
		return capacity;
	}

	/**
	 * @param capacity
	 *            the capacity to set
	 */
	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

/*	public int getLimit() {

		if (null != pageNum && null != start) {
			throw new AppException(StatusCode.ILLEGAL_ARGUMENT);
		}

		if (null != start) {
			return start;
		}

		if (null != pageNum) {
			if (1 > pageNum) {
				return 0;
			}

			return (pageNum - 1) * getOffset();
		}

		return 0;
	}

	public int getOffset() {

		int offset = (0 == capacity ? PropertyConst.DEFAULT_CAPACITY : capacity);

		return offset;
	}
*/
	/**
	 * @return the versionName
	 */
	public String getVersionName() {
		return versionName;
	}

	/**
	 * @param versionName
	 *            the versionName to set
	 */
	public void setVersionName(String versionName) {
		this.versionName = versionName;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet
	 * .ServletContext)
	 */
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	/**
	 * @return the servletContext
	 */
	public ServletContext getServletContext() {
		return servletContext;
	}

	/**
	 * @return the start
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * @param start
	 *            the start to set
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

}
